package cn.stylefeng.guns.modular.time.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.stylefeng.guns.modular.time.entity.Notification;
import cn.stylefeng.guns.modular.time.mapper.AppUserMapper;
import cn.stylefeng.guns.modular.time.mapper.NotificationMapper;
import cn.stylefeng.guns.modular.time.model.request.NotificationRequest;
import cn.stylefeng.guns.modular.time.service.NotificationService;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import jakarta.annotation.Resource;

import org.springframework.stereotype.Service;
import java.util.Date;

@Service
public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Notification> implements NotificationService {
   @Resource
   private AppUserMapper appUserMapper;

    @Override
    public PageResult<Notification> findPage(NotificationRequest request) {
        Long userId = LoginContext.me().getLoginUser() != null ? LoginContext.me().getLoginUser().getUserId() : null;
        Long uid = appUserMapper.getAppUserId(userId);
        LambdaQueryWrapper<Notification> qw = new LambdaQueryWrapper<>();
        qw.eq(Notification::getDelFlag, "N")
          .orderByDesc(Notification::getCreateTime);
        // 仅查询当前登录用户自己的消息
        if (uid != null) {
            qw.eq(Notification::getUserId, uid);
        } else {
            // 无登录用户则返回空页
            Page<Notification> emptyPage = new Page<>(request.getPageNo(), request.getPageSize());
            emptyPage.setRecords(java.util.Collections.emptyList());
            emptyPage.setTotal(0);
            return PageResultFactory.createPageResult(emptyPage);
        }
        Page<Notification> page = this.page(new Page<>(request.getPageNo(), request.getPageSize()), qw);
        return PageResultFactory.createPageResult(page);
    }

    @Override
    public void add(NotificationRequest request) {
        Long uid = LoginContext.me().getLoginUser() != null ? LoginContext.me().getLoginUser().getUserId() : null;
        // 用户ID为空时不保存
        if (uid == null) { return; }
        Notification n = new Notification();
        n.setNotificationId(null);
        n.setUserId(uid);
        n.setTitle(request.getTitle());
        n.setContent(request.getContent());
        n.setAiContent(request.getAiContent());
        n.setSourceUrl(request.getSourceUrl());
        n.setType(request.getType());
        n.setUniqueId(request.getUniqueId());
        n.setDetails(request.getDetails());
        n.setDelFlag("N");
        n.setCreateTime(new Date());
        this.save(n);
    }

    @Override
    public void delete(NotificationRequest request) {
        this.removeById(request.getNotificationId());
    }

    @Override
    public Notification detail(NotificationRequest request) {
        return this.getById(request.getNotificationId());
    }

    @Override
    public void addSystemNotification(String title, String content, String aiContent, String sourceUrl, String type) {
        // 不保存无用户ID的系统通知
        return;
    }

    /**
     * 添加系统通知，带唯一标识与详情
     */
    @Override
    public void addSystemNotification(String title, String content, String aiContent, String sourceUrl, String type, String uniqueId, String details) {
        // 不保存无用户ID的系统通知（按需求）
        return;
    }

    /**
     * 为指定用户添加通知，带唯一标识与详情
     */
    @Override
    public void addUserNotification(Long userId, String title, String content, String aiContent, String sourceUrl, String type, String uniqueId, String details) {
        // 用户ID为空时不保存
        if (userId == null) { return; }
        Notification n = new Notification();
        n.setNotificationId(null);
        n.setUserId(userId);
        n.setTitle(title);
        n.setContent(content);
        n.setAiContent(aiContent);
        n.setSourceUrl(sourceUrl);
        n.setType(type);
        n.setUniqueId(uniqueId);
        n.setDetails(details);
        n.setDelFlag("N");
        n.setCreateTime(new Date());
        this.save(n);
    }

    /**
     * 为指定用户添加通知并返回ID，带唯一标识与详情
     */
    @Override
    public Long addUserNotificationReturnId(Long userId, String title, String content, String aiContent, String sourceUrl, String type, String uniqueId, String details) {
        // 用户ID为空时不保存
        if (userId == null) { return null; }
        Notification n = new Notification();
        n.setNotificationId(null);
        n.setUserId(userId);
        n.setTitle(title);
        n.setContent(content);
        n.setAiContent(aiContent);
        n.setSourceUrl(sourceUrl);
        n.setType(type);
        n.setUniqueId(uniqueId);
        n.setDetails(details);
        n.setDelFlag("N");
        n.setCreateTime(new Date());
        this.save(n);
        return n.getNotificationId();
    }
}